package ml.coursedesign.controller;

import ml.coursedesign.common.lang.Result;
import ml.coursedesign.common.lang.StatusCode;
import ml.coursedesign.entity.CourseClass;
import ml.coursedesign.service.CourseClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 前端控制器：课程班级
 *
 * @author likeqc.top
 * @since 2021-04-27
 */
@RestController
@RequestMapping("/course-class")
public class CourseClassController {

  @Autowired
  private CourseClassService courseClassService;

  /**
   * 根据id删除课程班级
   *
   * @param classId 课程班级id
   * @return
   */
  @DeleteMapping("/{classId}")
  public Result remove(@PathVariable Integer classId) {
    courseClassService.removeById(classId);
    return Result.succ(null);
  }

  /**
   * 编辑或修改课程班级
   *
   * @param courseClass 课程班级实体
   * @return
   */
  @PostMapping("/edit")
  public Result edit(@RequestBody CourseClass courseClass) {
    // TODO: 权限验证，管理员才有资格编辑课程信息
    System.out.println(courseClass);
    System.out.println("课程id --- " + courseClass.getCourseId());
    System.out.println("教师id --- " + courseClass.getTeacherId());
    String msg = null;
    if (courseClass.getId() == null || courseClassService.getById(courseClass.getId()) == null) {
      // 新增
      courseClassService.save(courseClass);
      msg = "新增成功";
    } else {
      // 修改
      courseClassService.updateById(courseClass);
      msg = "修改成功";
    }
    return Result.succ(StatusCode.OK, msg, courseClass);
  }

  /**
   * 根据id返回课程班级信息
   *
   * @param id 课程班级id
   * @return
   */
  @GetMapping("/{id}")
  public Result getById(@PathVariable String id) {
    CourseClass courseClass = courseClassService.getById(id);
    return Result.succ(courseClass);
  }
  
}
